package com.liveinstars.gateway.application.endpoint;

import com.liveinstars.gateway.business.entity.ChangeSecretKeyDTO;
import com.liveinstars.gateway.business.service.AppAuthService;
import com.onepiece.shipelves.common.Result;
import com.onepiece.shipelves.common.exception.AuthException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

@RestController
public class AuthEndPoint {

    @Autowired
    @Qualifier("appAuthService")
    private AppAuthService appAuthService;

    @RequestMapping(path = "/changePublicSecretKey", method = RequestMethod.POST)
    @ResponseBody
    public Result changePublicSecretKey(@RequestBody ChangeSecretKeyDTO secretKeyDTO) {
        Result result = new Result();
        try {
            String s = appAuthService.changeSecretKey(secretKeyDTO);
            result.setData(s);
            result.setCode("200");
            result.setMessage("SUCCESS");
        } catch (Exception e) {
            if (e instanceof AuthException) {
                result.setCode(((AuthException) e).getCode());
                result.setMessage(((AuthException) e).getMsg());
            }
        }
        return result;
    }

    @RequestMapping(path = "/needChangeSecretKey", method = RequestMethod.POST)
    @ResponseBody
    public Result needChangeSecretKey(@RequestBody ChangeSecretKeyDTO secretKeyDTO) {
        Result result = new Result();
        if (appAuthService.needChangeSecretKey(secretKeyDTO.getAppId(), secretKeyDTO.getSecretKeyVersion())) {
            result.setData(true);
        } else {
            result.setData(false);
        }
        result.setCode("200");
        result.setMessage("SUCCESS");
        return result;
    }

    @RequestMapping(path = "/test", method = RequestMethod.GET)
    public Result test() {
        Result result = new Result();
        result.setCode("200");
        result.setMessage("SUCCESS");
        return result;
    }
}
